package diseaseandcureadministration;

import java.util.ArrayList;

import diseaseandcureadministration.cure.Cure;
import diseaseandcureadministration.disease.Disease;
import diseaseandcureadministration.disease.DiseaseRegistry;
import diseaseandcureadministration.registry.Registrable;
import diseaseandcureadministration.symptom.Symptom;

public class Doctor 
{	
	
	public static Cure diagnose(Patient patient)
	{
		ArrayList<Symptom> patientSymptoms = patient.getSymptoms();
		Cure cure = null;
		
		if(patientSymptoms.size() == 0)
		{
			return cure;
		}
		
		ArrayList<Disease> allDiseases = new ArrayList<Disease>();
		for(Registrable element: DiseaseRegistry.getRegistryInstance().getAll())
		{
			Disease disease = (Disease) element;
			allDiseases.add(disease);
		}
		
		ArrayList<Disease> possibleDiseases = new ArrayList<Disease>();
		
		for(Disease disease: allDiseases)
		{			
			if(disease.getSymptoms().containsAll(patientSymptoms))
			{
				possibleDiseases.add(disease);
			}		
		}
		
		if(possibleDiseases.size() == 1)
		{
			cure = possibleDiseases.get(0).getCure();
		}
		else if(possibleDiseases.size() > 1)
		{
			Disease bestMatch = possibleDiseases.get(0);
			
			for(int i = 1; i < possibleDiseases.size(); i ++)
			{
				//Since I know each diseases contains at least all the patients 
				//symptoms the size is a way to evaluate additional symptoms
				if(bestMatch.getSymptoms().size() > possibleDiseases.get(i).getSymptoms().size())
				{
					bestMatch = possibleDiseases.get(i);
				}
			}
			
			cure = bestMatch.getCure();
		}		
		
		return cure;
	}

}
